<template>
  <div class="sub-categroy">
    <div class="container">
      <!-- 面包屑区域 -->
      <SubBread/>
      <!-- 商品筛选区域 -->
      <SubFilter @filter-change="filterChange"/>
      <!-- 商品列表区域，排序+商品 -->
      <div class="goods-list">
        <!-- 排序组件 -->
        <SubSort @sort-change="sortChange"/>
        <!-- 商品组件 -->
        <ul>
          <li v-for="goods in goodsList" :key="goods.id" >
            <GoodsItem :goods="goods"/>
          </li>
        </ul>
        <!-- 无限加载组件 -->
        <XtxInfiniteLoading :loading="loading" :finished="finished" @infinite="getData"></XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>
<script>
import SubBread from './components/sub-bread'
import SubFilter from './components/sub-filter'
import SubSort from './components/sub-sort'
import GoodsItem from './components/goods-item'
import { ref, watch } from 'vue'
import { findSubCategoryGoods } from '@/api/category'
import { useRoute } from 'vue-router'
export default {
  name: 'SubCategory',
  components: {
    SubBread,
    SubFilter,
    SubSort,
    GoodsItem
  },
  setup () {
    const route = useRoute()
    // 控制XtxInfiniteLoading是否显示加载中状态数据
    const loading = ref(false)
    // 控制XtxInfiniteLoading是否显示加载完毕的数据
    const finished = ref(false)
    // 定义请求携带的对象数据,页码、条数
    let params = {
      page: 1,
      pageSize: 20
    }
    // 定义初始商品数据
    const goodsList = ref([])
    // 请求列表商品数据逻辑
    const getData = () => {
      // 显示加载中状态
      loading.value = true
      // 根据当前2级分类ID请求
      params.categoryId = route.params.id
      findSubCategoryGoods(params).then(({ result }) => {
        // 有数据就追加数据
        if (result.items.length) {
          // 每次请求时都增加页码
          params.page++
          loading.value = false
          goodsList.value.push(...result.items)
        } else {
          // 当请求数据全部加载完后，显示加载完状态
          finished.value = true
          loading.value = false
        }
      })
    }

    // 监听切换不同2级分类时，重新请求数据
    watch(() => route.params.id, (newVal) => {
      if (newVal && `/category/sub/${newVal}` === route.path) {
        finished.value = false
        // 清空商品列表，此时indSubCategoryGoods组件会自动顶到可视区从而触发请求数据函数
        goodsList.value = []
        // 页码调成1
        params = {
          page: 1,
          pageSize: 20
        }
      }
    })

    // 监听更换排序、勾选选项时整合对应请求参数，重新请求商品数据
    const sortChange = (sortParams) => {
      finished.value = false
      // 重置页码为1，并且加上排序、勾选信息,清空商品列表
      params = { ...sortParams, ...params }
      params.page = 1
      goodsList.value = []
    }

    // 监听更换筛选信息时整合对应请求参数，重新请求商品数据
    const filterChange = (filterParams) => {
      finished.value = false
      params = { ...filterParams, ...params }
      params.page = 1
      goodsList.value = []
    }
    return { loading, finished, getData, goodsList, sortChange, filterChange }
  }

}
</script>
<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
